home *** CD-ROM | disk | FTP | other *** search
- import java.applet.Applet;
- import java.awt.Color;
- import java.awt.Graphics;
-
- class InputField {
- int maxchars = 50;
- int cursorPos;
- Applet app;
- String sval;
- char[] buffer;
- int nChars;
- int width;
- int height;
- Color bgColor;
- Color fgColor;
-
- public InputField(String initValue, Applet app, int width, int height, Color bgColor, Color fgColor) {
- this.width = width;
- this.height = height;
- this.bgColor = bgColor;
- this.fgColor = fgColor;
- this.app = app;
- this.buffer = new char[this.maxchars];
- this.nChars = 0;
- if (initValue != null) {
- initValue.getChars(0, initValue.length(), this.buffer, 0);
- this.nChars = initValue.length();
- }
-
- this.sval = initValue;
- }
-
- public void setText(String val) {
- for(int i = 0; i < this.maxchars; ++i) {
- this.buffer[i] = 0;
- }
-
- this.sval = new String(val);
- if (val == null) {
- this.sval = "";
- this.nChars = 0;
- this.buffer[0] = 0;
- } else {
- this.sval.getChars(0, this.sval.length(), this.buffer, 0);
- this.nChars = val.length();
- this.sval = new String(this.buffer);
- }
- }
-
- public String getValue() {
- return this.sval;
- }
-
- public void paint(Graphics g, int x, int y) {
- g.setColor(this.bgColor);
- g.fillRect(x, y, this.width, this.height);
- if (this.sval != null) {
- g.setColor(this.fgColor);
- g.drawString(this.sval, x, y + this.height / 2 + 3);
- }
-
- }
-
- public void mouseUp(int x, int y) {
- }
-
- public void keyDown(int key) {
- if (this.nChars < this.maxchars) {
- switch (key) {
- case 8:
- --this.nChars;
- if (this.nChars < 0) {
- this.nChars = 0;
- }
-
- this.buffer[this.nChars] = 0;
- this.sval = new String(new String(this.buffer));
- break;
- case 9:
- default:
- this.buffer[this.nChars++] = (char)key;
- this.sval = new String(new String(this.buffer));
- break;
- case 10:
- this.selected();
- }
- }
-
- this.app.repaint();
- }
-
- public void selected() {
- }
- }
-